#!/bin/sh
# Usage: event.sh <event> <if-name>

case "$1" in
if-create)
	iptables -I FORWARD -o "$2" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
	;;
if-up)
	udhcpc -b -p /var/run/udhcpc."$2".pid -i "$2" -s /opt/etc/udhcpc/udhcpc.script
	iptables -t nat -A POSTROUTING -o "$2" ! -s $(ifconfig "$2" | sed -n 's/^.*inet addr:\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)  .*$/\1/p') -j MASQUERADE
	;;
if-down)
	iptables -t nat -D POSTROUTING -o "$2" ! -s $(ifconfig "$2" | sed -n 's/^.*inet addr:\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)  .*$/\1/p') -j MASQUERADE
	kill -TERM $(cat /var/run/udhcpc."$2".pid)
	;;
if-release)
	iptables -D FORWARD -o "$2" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
	;;
*)
	echo "Usage: $0 { if-create | if-up | if-down | if-release }" >&2
	exit 3
	;;
esac

